﻿using System;
using UnityEngine;

// Token: 0x02000127 RID: 295
[AddComponentMenu("RVP/C#/Damage/Detachable Part", 1)]
[DisallowMultipleComponent]
public class DetachablePart : MonoBehaviour
{
	// Token: 0x0600056E RID: 1390 RVA: 0x00029EE4 File Offset: 0x000280E4
	private void Start()
	{
		this.tr = base.transform;
		if (this.tr.parent)
		{
			this.initialParent = this.tr.parent;
			this.initialLocalPos = this.tr.localPosition;
			this.initialLocalRot = this.tr.localRotation;
		}
		this.parentBody = (Rigidbody)F.GetTopmostParentComponent<Rigidbody>(this.tr);
		this.initialPos = this.tr.localPosition;
	}

	// Token: 0x0600056F RID: 1391 RVA: 0x00029F6C File Offset: 0x0002816C
	private void Update()
	{
		if (this.hinge && (this.initialAnchor - this.displacedAnchor).sqrMagnitude > 0.1f)
		{
			UnityEngine.Object.Destroy(this.hinge);
		}
	}

	// Token: 0x06000570 RID: 1392 RVA: 0x00029FB8 File Offset: 0x000281B8
	public void Detach(bool makeJoint)
	{
		if (!this.detached)
		{
			this.detached = true;
			this.tr.parent = null;
			this.rb = base.gameObject.AddComponent<Rigidbody>();
			this.rb.mass = this.mass;
			this.rb.drag = this.drag;
			this.rb.angularDrag = this.angularDrag;
			if (this.parentBody)
			{
				this.parentBody.mass -= this.mass;
				this.rb.velocity = this.parentBody.GetPointVelocity(this.tr.position);
				this.rb.angularVelocity = this.parentBody.angularVelocity;
				if (makeJoint && this.joints.Length > 0)
				{
					PartJoint partJoint = this.joints[UnityEngine.Random.Range(0, this.joints.Length)];
					this.initialAnchor = partJoint.hingeAnchor;
					this.displacedAnchor = this.initialAnchor;
					this.hinge = base.gameObject.AddComponent<HingeJoint>();
					this.hinge.autoConfigureConnectedAnchor = false;
					this.hinge.connectedBody = this.parentBody;
					this.hinge.anchor = partJoint.hingeAnchor;
					this.hinge.axis = partJoint.hingeAxis;
					this.hinge.connectedAnchor = this.initialPos + partJoint.hingeAnchor;
					this.hinge.enableCollision = false;
					this.hinge.useLimits = partJoint.useLimits;
					JointLimits limits = default(JointLimits);
					limits.min = partJoint.minLimit;
					limits.max = partJoint.maxLimit;
					limits.bounciness = partJoint.bounciness;
					this.hinge.limits = limits;
					this.hinge.useSpring = partJoint.useSpring;
					JointSpring spring = default(JointSpring);
					spring.targetPosition = partJoint.springTargetPosition;
					spring.spring = partJoint.springForce;
					spring.damper = partJoint.springDamper;
					this.hinge.spring = spring;
					this.hinge.breakForce = this.breakForce;
					this.hinge.breakTorque = this.breakForce;
				}
			}
		}
	}

	// Token: 0x06000571 RID: 1393 RVA: 0x0002A1FC File Offset: 0x000283FC
	public void Reattach()
	{
		if (this.detached)
		{
			this.detached = false;
			this.tr.parent = this.initialParent;
			this.tr.localPosition = this.initialLocalPos;
			this.tr.localRotation = this.initialLocalRot;
			if (this.parentBody)
			{
				this.parentBody.mass += this.mass;
			}
			if (this.hinge)
			{
				UnityEngine.Object.Destroy(this.hinge);
			}
			if (this.rb)
			{
				UnityEngine.Object.Destroy(this.rb);
			}
		}
	}

	// Token: 0x06000572 RID: 1394 RVA: 0x0002A2AC File Offset: 0x000284AC
	private void OnDrawGizmosSelected()
	{
		if (!this.tr)
		{
			this.tr = base.transform;
		}
		if (this.looseForce >= 0f && this.joints.Length > 0)
		{
			Gizmos.color = Color.red;
			foreach (PartJoint partJoint in this.joints)
			{
				Gizmos.DrawRay(this.tr.TransformPoint(partJoint.hingeAnchor), this.tr.TransformDirection(partJoint.hingeAxis).normalized * 0.2f);
				Gizmos.DrawWireSphere(this.tr.TransformPoint(partJoint.hingeAnchor), 0.02f);
			}
		}
	}

	// Token: 0x0400078D RID: 1933
	private Transform tr;

	// Token: 0x0400078E RID: 1934
	private Rigidbody rb;

	// Token: 0x0400078F RID: 1935
	private Rigidbody parentBody;

	// Token: 0x04000790 RID: 1936
	private Transform initialParent;

	// Token: 0x04000791 RID: 1937
	private Vector3 initialLocalPos;

	// Token: 0x04000792 RID: 1938
	private Quaternion initialLocalRot;

	// Token: 0x04000793 RID: 1939
	[NonSerialized]
	public HingeJoint hinge;

	// Token: 0x04000794 RID: 1940
	[NonSerialized]
	public bool detached;

	// Token: 0x04000795 RID: 1941
	[NonSerialized]
	public Vector3 initialPos;

	// Token: 0x04000796 RID: 1942
	public float mass = 0.1f;

	// Token: 0x04000797 RID: 1943
	public float drag;

	// Token: 0x04000798 RID: 1944
	public float angularDrag = 0.05f;

	// Token: 0x04000799 RID: 1945
	public float looseForce = -1f;

	// Token: 0x0400079A RID: 1946
	public float breakForce = 25f;

	// Token: 0x0400079B RID: 1947
	[Tooltip("A hinge joint is randomly chosen from the list to use")]
	public PartJoint[] joints;

	// Token: 0x0400079C RID: 1948
	private Vector3 initialAnchor;

	// Token: 0x0400079D RID: 1949
	[NonSerialized]
	public Vector3 displacedAnchor;
}
